// 
//  MyClass.cs
//  
//  Author:
//       BOUTRY Arnaud <lordphoenix@tuxfamily.org>
// 
//  Copyright (c) 2010 BOUTRY Arnaud as Lordphoenix
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 

using System;
using System.IO;
using System.Reflection;
using System.Net;
using System.Xml;
using System.Security.Cryptography.X509Certificates;
using Gtk;
using Mono.Unix;

using swmt.swmtProvider;

[assembly:PluginInfo("gmail","gmail.gMail")]

namespace swmt.swmtProvider.gmail
{
	internal class GoogleCertificatePolicy : ICertificatePolicy
	{
		public GoogleCertificatePolicy ()
		{
		}
		public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
		{
			return true;
		}
	}

	public class gMail:WebService
	{
		private string _username;
		private string _password;
		private string _configfile;
		
		private int _lastcount;
		private string xmlResponse;

		public gMail ()
		{
			ServicePointManager.CertificatePolicy = new GoogleCertificatePolicy ();
			_lastcount = 0;
			Catalog.Init("swmt-plugins","");
		}
		public override bool connect ()
		{
			string url ="https://mail.google.com/mail/feed/atom";
			HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
			req.Method="POST";
			req.Credentials = new NetworkCredential(_username,_password);
			WebResponse resp = req.GetResponse();
			StreamReader read = new StreamReader(resp.GetResponseStream());
			xmlResponse = read.ReadToEnd();
			return(resp!=null);
		}
		
		public override void deleteconfigfile ()
		{
			if(File.Exists(_configfile))
				File.Delete(_configfile);
		}
		public override int GetUnreadCount ()
		{
			connect();
			XmlDocument xdoc = new XmlDocument();
			xdoc.LoadXml(xmlResponse);
			XmlNodeList nodelist = xdoc.DocumentElement.GetElementsByTagName("fullcount");
			var countnode = (XmlElement)nodelist[0];
			_lastcount = Int16.Parse(countnode.ChildNodes[0].Value);
			return _lastcount;
			
		}
		public override void loadconfig (string filename)
		{
			_configfile = filename;
			FileStream fichier = File.Open(_configfile,FileMode.Open);
			StreamReader r = new StreamReader(fichier);
			r.BaseStream.Seek(0,SeekOrigin.Begin);
			while(!r.EndOfStream){
				string optionstring = r.ReadLine();
				if(!optionstring.StartsWith("#")){
					int cpos = optionstring.IndexOf("=");
					if(optionstring.StartsWith("username"))
						_username = optionstring.Substring(cpos+1,optionstring.Length-cpos-1);
					if(optionstring.StartsWith("password"))
						_password = optionstring.Substring(cpos+1,optionstring.Length-cpos-1);
				}
			}
			r.Close();
			fichier.Close();
		}
		public override void saveconfig (string filename)
		{
			_configfile = filename;
			FileStream fichier = File.Open(_configfile,FileMode.OpenOrCreate);
			StreamWriter w = new StreamWriter(fichier);
			w.BaseStream.Seek(0,SeekOrigin.Begin);
			string optionstring = String.Format("username={0}",_username);
			w.WriteLine(optionstring);
			optionstring = String.Format("password={0}",_password);
			w.WriteLine(optionstring);
			w.Flush();
			fichier.Flush();
			w.Close();
			fichier.Close();
		}
		public override ResponseType Prompt (bool edit)
		{
			GmailAccount dlg = new GmailAccount();
			if(edit){
				dlg.username = _username;
				dlg.password = _password;
			}
			var result = (ResponseType)dlg.Run();
			if(result == ResponseType.Ok){
				_username = dlg.username;
				_password = dlg.password;
			}
			dlg.Hide();
			return result;
		}
		public override int LastCount {
			get {
				return _lastcount;
			}
		}
		public override string pluginfile {
			get {
				Assembly plug = Assembly.GetExecutingAssembly ();
				return plug.GetName ().Name+".dll";
			}
		}
		public override string servicename {
			get {
				return("gMail");
			}
		}
		public override string Homepage {
			get {
				return "http://mail.google.com";
			}
		}




	}
}
